package com.jason.kotlin.meta.architecture1.api

import com.jason.kotlin.meta.architecture1.model.MetaImageBean
import com.jason.kotlin.meta.architecture1.model.MetaResponseBean
import retrofit2.Call
import retrofit2.Response
import java.util.*

/**
 * @author chenbaoyang
 */
class MetaImageRepository {

    companion object {
        /**
         * 搜索关键字
         */
        const val KEY_WORD = "q"

        /**
         * 页数
         */
        const val PAGE = "sn"

        /**
         * 每页个数
         */
        const val COUNT = "PN"
    }


    /**
     * 搜索meta的图片资源
     *
     * @param keyWord String
     * @param page Int
     * @param count Int
     */
    fun loadMetaImageList(
        keyWord: String,
        page: Int,
        count: Int,
        callback: (list: MutableList<MetaImageBean>) -> Unit
    ){
        val map: MutableMap<String, String> = HashMap()
        map[KEY_WORD] = keyWord
        map[PAGE] = page.toString()
        map[COUNT] = count.toString()
        return ApiSource.getMetaService(MetaApiService::class.java).loadMetaImages(map).enqueue(
            object : retrofit2.Callback<MetaResponseBean<List<MetaImageBean>>> {
                override fun onFailure(
                    call: Call<MetaResponseBean<List<MetaImageBean>>>,
                    t: Throwable
                ) {
                    callback(mutableListOf())
                }

                override fun onResponse(
                    call: Call<MetaResponseBean<List<MetaImageBean>>>,
                    response: Response<MetaResponseBean<List<MetaImageBean>>>
                ) {
                    callback(response.body()?.list as MutableList<MetaImageBean>)
                }

            })
    }


}